#include "FovCell.h"
#include <QPainter>
#include <QEvent>
#include <QGraphicsSceneMouseEvent>

FovCell::FovCell(int regionRow, int regionCol, int fovRow, int fovCol, QSize fovSize, QObject  *parent):
    QObject(parent),
    QGraphicsItem(nullptr),
    itemRow(regionRow),
    itemCol(regionCol),
    fovRow(fovRow),
    fovCol(fovCol),
    size(fovSize)
{
    setAcceptHoverEvents(true);
    setFlag(QGraphicsItem::ItemIsFocusable); // 允许获得焦点
    result = -1;

}

QRectF FovCell::boundingRect() const
{
    return QRectF(0,0,size.width(),size.height());
}

void FovCell::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    painter->setPen(QPen(Qt::black,1));

    if(result == 1)
        painter->setBrush(QBrush(Qt::red));
    else if(result == 0)
        painter->setBrush(QBrush(Qt::green));
    else
        painter->setBrush(QBrush(Qt::transparent));

    painter->drawRect(boundingRect().adjusted(0, 0, -1, -1));

    update();
}

void FovCell::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
    qDebug()<<"Fov clicked ";
    emit fovClicked(itemRow,itemCol,fovRow,fovCol);
    event->accept();
}
